package svc

import (
	"github.com/zeromicro/go-zero/core/bloom"
	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"short/internal/config"
	"short/model"
	"short/sequence"
)

type ServiceContext struct {
	Config        config.Config
	ShortUrl      model.ShortUrlMapModel
	Sequence      *sequence.MySQL
	ShortBlackmap map[string]struct{} //短链黑名单
	Filter        *bloom.Filter
}

func NewServiceContext(c config.Config) *ServiceContext {
	conn := sqlx.NewMysql(c.ShortUrlDB.DSN)
	m := make(map[string]struct{}, len(c.ShortBlackmap))
	store, err := redis.NewRedis(c.CacheRedis[0].RedisConf, func(r *redis.Redis) {
		r.Type = redis.NodeType
	})
	if err != nil {
		panic(err)
	}
	filter := bloom.New(store, "bloom_redis", 20*(1<<20))

	return &ServiceContext{
		Config:        c,
		ShortUrl:      model.NewShortUrlMapModel(conn, c.CacheRedis),
		Sequence:      sequence.NewMySQL(c.Sequence.DSN),
		ShortBlackmap: m,
		Filter:        filter,
	}
}

// 加载数据库里已有的短链
func LoadUrl() {

}
